package com.example.zlw.dem13;



import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: Zlw
 * @description:
 * @author: 诸葛龙辰
 * @create: 2022-11-06 18:13
 **/
public class Test2 {
    public static void main(String[] args) {

        List<Pig> list = new ArrayList<>();

        list.add(new Pig("佩奇",1,"郑州"));
        list.add(new Pig("野猪佩奇",11,"郑州"));
        list.add(new Pig("黑猪佩奇",21,"郑州"));
        list.add(new Pig("野猪王佩奇",3,"郑州"));
        list.add(new Pig("黑猪王佩奇",5,"郑州"));
        list.add(new Pig("骚猪佩奇",7,"郑州"));
        list.add(new Pig("傻猪佩奇",9,"郑州"));
        list.add(new Pig("烤佩奇",10,"郑州"));
        list.add(new Pig("炖佩奇",15,"郑州"));
        list.add(new Pig("铁板佩奇",18,"郑州"));


        System.out.println("============================Stream流中的操作==========================");
        System.out.println("============================Stream中间操作==========================");
        /*filter筛选操作*/
        System.out.println("============================filter筛选操作==========================");
        list.stream().filter(item->item.getAge()>5).forEach(System.out::println);
        /*map映射操作*/
        System.out.println("============================map映射操作==========================");
        list.stream().map(item -> item.getName()).forEach(System.out::println);
        /*sort排序操作*/
        System.out.println("============================sort排序操作==========================");
        list.stream().sorted((o1, o2) -> o1.getAge() - o2.getAge()).forEach(System.out::println);
        String arr[]={"1","6","3","2","5"};
        String arr1[]={"5","6","2","3","1"};
        Stream<String> arr2 = Stream.of(arr);
        Stream<String> arr3 = Stream.of(arr1);
        /*concat合并*/
        System.out.println("============================cancat合并与操作==========================");
//        Stream.concat(arr2,arr3).forEach(System.out::println);
        /*distinct去重*/
        System.out.println("============================distinct去重==========================");
//        Stream.concat(arr2,arr3).distinct().forEach(System.out::println);
        /*limit只要截取数据个数*/
        System.out.println("============================limit只要截取数据个数==========================");
//        Stream.concat(arr2,arr3).limit(4).forEach(System.out::println);
        /*skip去除数据中几个数据*/
        System.out.println("============================skip去除数据中几个数据==========================");
        Stream.concat(arr2,arr3).skip(4).forEach(System.out::println);
        System.out.println("============================find操作==========================");
        /*findFirst获取序列中第一条数据*/
        System.out.println("============================findFirst获取序列中第一条数据==========================");
        Optional<Pig> first = list.stream().findFirst();
        System.out.println(first);
        /*findAny随机获取序列中的一条数据*/
        System.out.println("============================findAny随机获取序列中的一条数据==========================");
        Optional<Pig> any = list.stream().findAny();
        System.out.println(any);
        System.out.println("============================match操作==========================");
        /*allMatch所有条件均需满足才能实现*/
        System.out.println("============================allMatch所有条件均需满足才能实现==========================");
        boolean b = list.stream().allMatch(item -> item.getAge() > 3);
        System.out.println(b);
        /*anyMatch随机条件均需满足才能实现*/
        System.out.println("============================anyMatch随机条件均需满足才能实现==========================");
        boolean b1 = list.stream().anyMatch(item -> item.getAge() > 3);
        System.out.println(b1);
        System.out.println("============================count聚合操作==========================");
        /*max最大值*/
        System.out.println("============================max最大值==========================");
        Optional<Pig> max = list.stream().max((o1,o2) -> o1.getAge()-o2.getAge());
        System.out.println(max);
        /*min最小值*/
        System.out.println("============================max最大值==========================");
        Optional<Pig> min = list.stream().min((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println(min);
        /*count总数量*/
        System.out.println("============================max最大值==========================");
        long count = list.stream().count();
        System.out.println(count);
        Integer [] arr0={2,3,4,5,6,2};
        System.out.println("============================reduce规约操作可以进行加减乘除还有最值==========================");
        Optional<Integer> reduce = Arrays.stream(arr0).reduce((o1, o2) -> o1 - o2);
        Optional<Integer> reduce1 = Arrays.stream(arr0).reduce((o1, o2) -> o1 + o2);
        Optional<Integer> reduce2 = Arrays.stream(arr0).reduce((o1, o2) -> o1 * o2);
        Optional<Integer> reduce3 = Arrays.stream(arr0).reduce((o1, o2) -> o1 / o2);
        System.out.println(reduce);
        System.out.println("============================collect搜集操作==========================");
        /*获取新的序列集合*/
        List<Pig> collect = list.stream().collect(Collectors.toList());
        /*获取新的map集合*/
        Map<String, Pig> collect1 = list.stream().collect(Collectors.toMap(item -> item.getName(), item -> item));
        /*分组*/
        Map<String, List<Pig>> collect2 = list.stream().collect(Collectors.groupingBy(item -> item.getName()));
        System.out.println(collect2);
        /*最大值*/
        Optional<Pig> collect3 = list.stream().collect(Collectors.maxBy((o1, o2) -> o1.getAge() - o2.getAge()));
        System.out.println(collect3);

    }
}
class Pig{
    private String name;
    private  int age;
    private String address;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "pig{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';

    }

    public Pig() {
    }

    public Pig(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }
}
